<?php
namespace app\shop\controller;
use think\Db;
use lib\UserCrypt;
class My extends Base{
    public function my(){
        if(request()->isGet()){
            $url=$this->request->domain();
            if(session('userinfo')){
                $cates=Db::name('cates')->where('f_id',0)->where('state',1)->select();
                $province=Db::name('city')->where('level',1)->select();
                $addresses=Db::name('address')->where('user_id',session('userinfo')['id'])->where('state','<',3)->select();
                $ordersubs=Db::name('ordersub')->where('user_id',session('userinfo')['id'])->order('id desc')->paginate(12,false,['query' => request()->param(),'fragment'=>'order']);
                $items = $ordersubs->items();
                if($items){
                    foreach ($items as $key => $value) {
                        $items[$key]['time']=date('Y-m-d H:i:s',$items[$key]['time']);
                        if($items[$key]['order_type']==1){
                            $items[$key]['img']=Db::name('product')->where('id',$items[$key]['p_id'])->value('img');
                        }elseif($items[$key]['order_type']==2){
                            $items[$key]['img']=Db::name('scenic_spot')->where('id',$items[$key]['p_id'])->value('img');
                        }
                    }
                }
                $wpay_count=Db::name('ordersub')->where('user_id',session('userinfo')['id'])->where('state',1)->count();
                $wfh_count=Db::name('ordersub')->where('user_id',session('userinfo')['id'])->where('state',2)->count();
                $wsh_count=Db::name('ordersub')->where('user_id',session('userinfo')['id'])->where('state',3)->count();
                $finish_count=Db::name('ordersub')->where('user_id',session('userinfo')['id'])->where('state',4)->count();
                if(!empty(input("param.address_id"))){
                    $addressDetail=Db::name('address')->where('user_id',session('userinfo')['id'])->where('id',input("param.address_id"))->where('state','<',3)->find();
                    $provinceInfo=Db::name('city')->where('areaName',$addressDetail['province'])->where('level',1)->find();
                    $city=Db::name('city')->where('parentId',$provinceInfo['areaId'])->select();
                    $cityInfo=Db::name('city')->where('areaName',$addressDetail['city'])->where('level',2)->find();
                    $district=Db::name('city')->where('parentId',$cityInfo['areaId'])->select();
                    $this->assign('city',$city);
                    $this->assign('district',$district);
                    $this->assign('address_id',input("param.address_id"));
                    $this->assign('addressDetail',$addressDetail);
                }else{
                    $this->assign('city',0);
                    $this->assign('district',0);
                    $this->assign('address_id',0);
                    $this->assign('addressDetail',0);
                }
                $in_money=Db::name('money_bill')->where('user_id',session('userinfo')['id'])->where('type',1)->sum('money');
                $out_money=Db::name('money_bill')->where('user_id',session('userinfo')['id'])->where('type',2)->sum('money');
                $withdraw_money=Db::name('money_bill')->where('user_id',session('userinfo')['id'])->where('type',3)->sum('money');
                $usersinfo=Db::name('users')->where('id',session('userinfo')['id'])->find();
                $my_teams=Db::name('users')->field('id,phone,headimgurl,nickname,time')->where('invite_id',session('userinfo')['id'])->paginate(10,false,['query' => request()->param(),'fragment'=>'distributionTeam']);
                $team_items = $my_teams->items();
                foreach ($team_items as $key => $value) {
                        $team_items[$key]['phone'] = substr_replace($team_items[$key]['phone'], '****', 3, 4);
                        $team_items[$key]['time']=date('Y-m-d H:i:s',$team_items[$key]['time']);
                        $team_items[$key]['nums']=Db::name('ordersub')->where('user_id',$team_items[$key]['id'])->where('state','in',[2,3,4])->count();
                }
                $distributor_orders=Db::name('money_bill')->alias('a')->join('ordersub b', 'a.orders_id = b.id','LEFT')->join('users c', 'b.user_id = c.id','LEFT')->field('a.*,b.p_id,b.title,b.order_type,b.pay_fee,b.buy_num,c.phone')->where('a.user_id',session('userinfo')['id'])->where('a.type',1)->order('a.id desc')->paginate(10,false,['query' => request()->param(),'fragment'=>'distributionOrder']);
                $distributor_orders_items = $distributor_orders->items();
                foreach ($distributor_orders_items as $key => $value) {
                    $distributor_orders_items[$key]['phone'] = substr_replace($distributor_orders_items[$key]['phone'], '****', 3, 4);
                    if($distributor_orders_items[$key]['order_type']==1){
                        $distributor_orders_items[$key]['img']=Db::name('product')->where('id',$distributor_orders_items[$key]['p_id'])->value('img');
                    }elseif($distributor_orders_items[$key]['order_type']==2){
                        $distributor_orders_items[$key]['img']=Db::name('scenic_spot')->where('id',$distributor_orders_items[$key]['p_id'])->value('img');
                    }
                }
                $withdraw_bills=Db::name('money_bill')->where('user_id',session('userinfo')['id'])->paginate(10,false,['query' => request()->param(),'fragment'=>'distributionDetail']);
                $withdraw_bills_items = $withdraw_bills->items();
                foreach ($withdraw_bills_items as $key => $value) {
                    $withdraw_bills_items[$key]['time']=date('Y-m-d H:i:s',$withdraw_bills_items[$key]['time']);
                    if($withdraw_bills_items[$key]['type']==1){
                        $withdraw_bills_items[$key]['des']=Db::name('ordersub')->where('id',$withdraw_bills_items[$key]['orders_id'])->value('title');
                    }elseif($withdraw_bills_items[$key]['type']==2){
                        $withdraw_bills_items[$key]['des']=Db::name('ordersub')->where('id',$withdraw_bills_items[$key]['orders_id'])->value('title');
                    }elseif($withdraw_bills_items[$key]['type']==3){
                        $withdraw_bills_items[$key]['des']='提现';
                    }
                }
                $this->assign('withdraw_bills',$withdraw_bills);
                $this->assign('withdraw_bills_items',$withdraw_bills_items);
                $this->assign('distributor_orders',$distributor_orders);
                $this->assign('distributor_orders_items',$distributor_orders_items);
                $this->assign('my_teams',$my_teams);
                $this->assign('team_items',$team_items);
                $this->assign('in_money',$in_money);//分销邀请挣的佣金
                $this->assign('out_money',$out_money);//消费
                $this->assign('withdraw_money',$withdraw_money);//提现
                $this->assign('my_money',$usersinfo['money']);//余额
                $this->assign('invite_code',$usersinfo['invite_code']);//余额
                $this->assign('cates',$cates);
                $this->assign('province',$province);
                $this->assign('addresses',$addresses);
                $this->assign('ordersubs',$ordersubs);
                $this->assign('wpay_count',$wpay_count);
                $this->assign('wfh_count',$wfh_count);
                $this->assign('wsh_count',$wsh_count);
                $this->assign('finish_count',$finish_count);
                $this->assign('items',$items);
                $this->assign('url',$url);
                $this->assign('system',$this->system_set());
                return $this->fetch();
            }else{
                $this->redirect(url('login/login'));
            }
        }elseif(request()->isPost()){

        }
    }
    //获取市、区
    public function cityDistrict()
    {
        $res=Db::name('city')->where('parentId',$_GET["areaId"])->select();
        if($res){
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
        }else{
            return json(['code' => 500, 'msg' => '获取失败！']);
        }
    }
    public function state_on(){
        $id=input('param.id');
        $state=input('param.state');
        if($state==1){
            $status = Db::name('address')->where(array('id'=>$id))->update(['state'=>2,'uptime'=>time()]);
            if($status){
                return json(['code' => 200,'msg' => '成功']);
            }else{
                return json(['code' => 500,'msg' => '出错']);
            }
        }else{
            if($state==2){
            Db::name('address')->where(array('user_id'=>session('userinfo')['id']))->where('state','<',3)->update(['state'=>2,'uptime'=>time()]);
            $status = Db::name('address')->where(array('id'=>$id))->update(['state'=>1,'uptime'=>time()]);
            if($status){
                return json(['code' => 200,'msg' => '成功']);
            }else{
                return json(['code' => 500,'msg' => '出错']);
            }
        }
        }
    }
    public function my_cart(){
        $token=input("param.token");
        $map = [
            ['state', '=', 1]
        ];
        $url=$this->request->domain();
        // $uid=UserCrypt::do_mdecrypt($token);
        if(session('userinfo')){
            $uid=session('userinfo')['id'];
            array_push($map,['user_id','=',$uid]);
            $cates=Db::name('cates')->where('f_id',0)->where('state',1)->select();
            $res=Db::name('carts')->field('id,user_id,p_id,spec_combination_id,buy_num,type')->where($map)->order('id desc')->select();
            if ($res) {
                foreach ($res as $key => $value) {
                    if($res[$key]['type']==1){
                        $res[$key]['title_img']=Db::name('spec_combination')->alias('a')->join('product b', 'a.p_id = b.id','LEFT')->field('a.combination,a.num,b.title,b.img,a.money as price,b.state as p_state')->where('a.id',$res[$key]['spec_combination_id'])->find();
                    }elseif($res[$key]['type']==2){
                        $res[$key]['title_img']=Db::name('scenic_spot')->field('title,img,price,state as p_state')->where('id',$res[$key]['p_id'])->find();
                        $res[$key]['title_img']['combination']='(景区门票)';
                        $res[$key]['title_img']['num']=0;
                    }
                }
            }
        }else{
            $this->redirect(url('login/login'));
        }
        
        $this->assign('cates',$cates);
        $this->assign('carts',$res);
        $this->assign('url',$url);
        return $this->fetch();
    }
    public function cart_del(){
        $param = input('post.');
        $res= Db::name('carts')->where(array('id'=>$param['id']))->update(['state'=>2]);
        return json(['code' => 200,'msg' => '删除成功']);

    }
}
